package com.yhw.srb.sms.service.impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.yhw.common.exception.BusinessException;
import com.yhw.srb.sms.service.SmsService;
import com.yhw.srb.sms.util.SmsProperties;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Set;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Override
    public void send(String mobile, String templateId, String[] datas) {

        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(SmsProperties.SERVER_IP, SmsProperties.SERVER_PPORT);
        sdk.setAccount(SmsProperties.ACCOUNT_SID, SmsProperties.ACCOUNT_TOKEN);
        sdk.setAppId(SmsProperties.APP_ID);
        sdk.setBodyType(BodyType.Type_JSON);

//        String subAppend="1234";  //可选 扩展码，四位数字 0~9999
//        String reqId="fadfafas";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复

        // mobile 手机号码     templateId  模板几     datas 验证码和有效时间
        HashMap<String, Object> result = sdk.sendTemplateSMS(mobile,templateId,datas);

        if("000000".equals(result.get("statusCode"))){   // statusCode 等于 000000  说明发送成功
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");

            Set<String> keySet = data.keySet();

            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }

        }else{
            //异常返回输出 错误码 和 错误信息
            String code = (String) result.get("statusCode");
            String message = (String) result.get("statusMsg");
            System.out.println("错误码=" + code +" 错误信息= "+message);
            throw new BusinessException(message, Integer.parseInt(code));
        }

    }

}
